package com.init.iblog.Controller;

import com.init.iblog.domain.ResponseResult;
import com.init.iblog.domain.dto.LinkDto;
import com.init.iblog.domain.entity.Link;
import com.init.iblog.domain.entity.User;
import com.init.iblog.domain.vo.PageVo;
import com.init.iblog.service.LinkService;
import com.init.iblog.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/content/link")
public class LinkController {


    @Autowired
    private LinkService linkService;

    @GetMapping("/list")
    public ResponseResult<PageVo> listLink(Integer pageNum, Integer pageSize, LinkDto linkDto){
        System.out.println("========进入分页查询友链接口============");
        return linkService.pageLinkList(pageNum,pageSize,linkDto);
    }

    @GetMapping("/{id}")
    public ResponseResult getLink(@PathVariable Long id){
        System.out.println("==========进入查询友链接口==========");
        return linkService.getLink(id);
    }

    /**
     * 改变友链状态
     * @param id
     * @param newStatus
     * @return
     */
    @PutMapping("/changeLinkStatus")
    public ResponseResult changeLinkStatus(@PathVariable String id,@PathVariable String newStatus){
        System.out.println("==========进入改变友链状态接口");
        return null;
    }

    /**
     * 新增友链
     * @param link
     * @return
     */
    @PostMapping
    public ResponseResult addLink(@RequestBody Link link){
        System.out.println("=========进入新增友链接口============");
        User user = SecurityUtils.getLoginUser().getUser();
        Long create_by = user.getId();
        link.setCreateBy(create_by);
        link.setCreateTime(new Date());
        return linkService.addLink(link);
    }

    /**
     * 修改友链
     */
    @PutMapping
    public ResponseResult updateLink(@RequestBody Link link){

        System.out.println("=======进入更新友链接口==========");
        return linkService.updateLink(link);
    }


    @DeleteMapping("/{id}")
    public ResponseResult delLink(@PathVariable List<Long> id){
        System.out.println("=========进入删除友链接口===========");

        return linkService.deleteLink(id);
    }

}
